package com.mf.basic.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mf.basic.entity.DtoDic;
import com.mf.basic.log_api.LogApi;
import com.mf.basic.services.ServiceDic;
import com.mf.basic.utils.MyAssert;
import com.mf.basic.entity.DtoDicCatalog;
import com.mf.basic.rs.ResponseResult;
import com.mf.basic.services.ServiceDicCatalog;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Luke on 2025/3/7 16:58.
 */
@RestController
@ResponseResult
@SaCheckLogin
@RequestMapping("/dic")
public class ControllerDic implements MyAssert {

    @Resource
    private ServiceDicCatalog serviceDicCatalog;
    @Resource
    private ServiceDic serviceDic;

    /**
     * 获取字典目录树
     * @return Map<String, List<DtoDicCatalog>>
     */
    @GetMapping("/catalogTree")
    @SaCheckPermission("dic.search")
    public Map<String, List<DtoDicCatalog>> getCatalogTree() {
        return serviceDicCatalog.findTree();
    }

    /**
     * 添加字典
     * @param dic 字典信息
     */
    @LogApi("字典-添加")
    @PostMapping("/add")
    @SaCheckPermission("dic.add")
    public void add(@RequestBody DtoDic dic) {
        idIsNull(dic.getCatalogId(), "目录ID");
        strIsNull(dic.getDicCode(), "目录编码");
        Assert.state(dic.getDicCode().length()<=50, "目录编码长度不能超过50");
        strIsNull(dic.getDicValue(), "字典值");
        serviceDic.doAdd(dic);
    }

    /**
     * 修改字典
     * @param dic 字典信息
     */
    @LogApi("字典-编辑")
    @PostMapping("/update")
    @SaCheckPermission("dic.update")
    public void update(@RequestBody DtoDic dic) {
        idIsNull(dic.getId(), "字典ID");
        serviceDic.doUpdate(dic);
    }

    /**
     * 启用字典
     * @param params 字典ID
     */
    @LogApi("字典-启用")
    @PostMapping("/enable")
    @SaCheckPermission("dic.manager")
    public void enable(@RequestBody Map<String, Object> params) {
        idIsNull(params.get("id"), "字典ID");
        serviceDic.upStatus(Long.parseLong(params.get("id").toString()), 0);
    }

    /**
     * 禁用字典
     * @param params 字典ID
     */
    @LogApi("字典-禁用")
    @PostMapping("/disable")
    @SaCheckPermission("dic.disable")
    public void disable(@RequestBody Map<String, Object> params) {
        idIsNull(params.get("id"), "字典ID");
        serviceDic.upStatus(Long.parseLong(params.get("id").toString()), 1);
    }

    /**
     * 根据字典目录编码查询字典列表
     * @param params 字典目录编码
     * @return List<DtoDic>
     */
    @SaCheckPermission("dic.search")
    @PostMapping("/listByCatalogCode")
    public Map<String, List<DtoDic>> listByCatalogCode(@RequestBody Map<String, Object> params) {
        String catalogCode = strIsNull(params.get("catalogCode"), "目录编码");
        return serviceDic.findByCatalogCode(catalogCode);
    }

}
